//
//  XJShangPinEvaluateSectionHeaderView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/6/11.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  商品评价 区头

#import "XJShangPinEvaluateSectionHeaderView.h"
#import "XJShangPinEvaluateModel.h"
#import "YYLabel.h"
#import "NSAttributedString+YYText.h"


@interface XJShangPinEvaluateSectionHeaderView()
/**商品评价Lb*/
@property(nonatomic, strong) UILabel * shangPinPingJiaLb;

/**好评率Lb*/
@property(nonatomic, strong) YYLabel * haoPinLvLb;



@end

@implementation XJShangPinEvaluateSectionHeaderView
#pragma mark - init
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithReuseIdentifier:reuseIdentifier];
    
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    [self.contentView addSubview:self.shangPinPingJiaLb];
    [self.contentView addSubview:self.haoPinLvLb];
}

#pragma mark - 二 lazy
- (UILabel *)shangPinPingJiaLb {
    if (!_shangPinPingJiaLb) {
        _shangPinPingJiaLb = [UILabel new];
        
        _shangPinPingJiaLb.text = @"商品评价";
        _shangPinPingJiaLb.font = XJFONTSET_M(17.0);
    }
    return _shangPinPingJiaLb;
}

- (YYLabel *)haoPinLvLb {
    if (!_haoPinLvLb) {
        _haoPinLvLb = [YYLabel new];
        _haoPinLvLb.textAlignment = NSTextAlignmentRight;
    }
    return _haoPinLvLb;
}

#pragma mark - layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.shangPinPingJiaLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(15);
        make.top.bottom.equalTo(self.contentView);
        make.width.mas_equalTo(120);
    }];
    
    [self.haoPinLvLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.shangPinPingJiaLb.mas_right).offset(15);
        make.right.equalTo(self.contentView.mas_right).offset(-15);
        make.centerY.equalTo(self.contentView.mas_centerY);
        make.height.mas_equalTo(20);
    }];
}

#pragma mark - 赋值
//- (void)configureShangPinEvaluateSectionHeaderViewWithEvaluateArr:(NSArray *)arr {
//    if (arr.count) {
//        
//        self.haoPinLvLb.text = [NSString stringWithFormat:@"好评率90%% 查看全部(322)"];
//    }else {
//        self.haoPinLvLb.text = @"暂无评论";
//    }
//}

- (void)configureShangPinEvaluateSectionHeaderViewWithPraise_rate:(NSString *)goodRate comment_count:(NSString *)count {
    
    if ([count isEqualToString:@"0"]) {
        self.haoPinLvLb.text = @"暂无评论";
        
    }else {
        
        UIColor *redColor = [UIColor redColor]; //红色
        UIColor *lightGrayColor = [UIColor lightGrayColor];   //灰色
        UIColor *clearColor = [UIColor clearColor];

        NSString *rateStr = [NSString stringWithFormat:@"好评率%@%%",goodRate];
        NSString *cntStr = [NSString stringWithFormat:@"查看全部(%@)",count];
        
        NSString *wholeStr = [NSString stringWithFormat:@"%@ %@ ",rateStr,cntStr];

        NSRange range1 = [wholeStr rangeOfString:rateStr];
        NSRange range2 = [wholeStr rangeOfString:cntStr];
        
        // 富文本
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:wholeStr attributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
        attStr.yy_font = XJFONTSET_R(17.0);
        attStr.yy_lineSpacing = 0;
        attStr.yy_alignment = NSTextAlignmentRight;

        [attStr yy_setTextHighlightRange:range1 color:redColor backgroundColor:clearColor tapAction:nil];
        [attStr yy_setTextHighlightRange:range2 color:lightGrayColor backgroundColor:clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            
            if (self.xjsppjSecHeadViewClickAllEvluteBlock) {
                self.xjsppjSecHeadViewClickAllEvluteBlock();
            }
        }];
        
        self.haoPinLvLb.attributedText = attStr;

    }
}

//- (void) fjdfj  {
//    UIColor *blueColor = [UIColor blueColor];
//    UIColor *clearColor = [UIColor clearColor];
//    
//    NSRange range1 = [wholeStr rangeOfString:subStr];
//    
//    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:wholeStr attributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
//    attributeStr.yy_font = [UIFont systemFontOfSize:14.0];
//    attributeStr.yy_lineSpacing = 0;
//    attributeStr.yy_alignment = NSTextAlignmentCenter;
//    
//    [attributeStr yy_setTextHighlightRange:range1 color:blueColor backgroundColor:clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
//        //XJLog(@"点击了 《xxxx充值协议》");
//        //回调点击事件
//        
//        if (self.clickChargeProtocalBlock) {
//            self.clickChargeProtocalBlock();
//        }
//    }];
//    
//    self.chargeProtocolLabel.attributedText = attributeStr;
//}

@end
